package cn.exercises.d05;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentSystem {
    private static final Scanner sc = new Scanner(System.in);
    private static final List<Student> list = new ArrayList<>();
    /*
    * 采取控制台的方式去书写学生管理系统。
    * */
    public static void main(String[] args) {
        startStudentSystem();
    }

    public static void startStudentSystem() {
        loop: while (true) {
            System.out.println("-----------------欢迎来到xx学生管理系统-------------------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请输入您的选择：");
            String choose = sc.next();
            switch (choose) {
                case "1" -> addStudent();
                case "2" -> deleteStudent();
                case "3" -> updateStudent();
                case "4" -> queryStudent();
                case "5" -> {
                    System.out.println("感谢使用本系统，再见！");
                    System.exit(0); //  退出程序
                    // break loop; // 跳出while循环
                }
               default -> System.out.println("没有这个选项");
            }
        }
    }

    private static void queryStudent() {
        if (list.isEmpty()){
            System.out.println("当前没有学生信息");
            return;
        }
        System.out.println("学号\t姓名\t\t年龄\t家庭住址");
        for (Student stu : list) {
            System.out.printf("%s\t%s\t\t%d\t%s\n",stu.getId(),stu.getName(),stu.getAge(),stu.getAddress());
        }
    }

    private static void updateStudent() {
        System.out.println("请输入要修改学生的id");
        String id = sc.next();
        int index = getIndex(id);
        if (index==-1){
            System.out.println("没有这个id是"+id+"的学生");
            return;
        }
        Student student = list.get(index);
        inputStudentInfo(student);
        System.out.println("更新成功");
    }

    private static void deleteStudent() {
        System.out.println("请输入要删除的id");
        String id = sc.next();
        int index = getIndex(id);
        if (index==-1){
            System.out.println("没有这个id是"+id+"的学生");
            return;
        }
        list.remove(index);
        System.out.println("删除成功");
    }

    /*
        键盘录入每一个学生信息并添加，需要满足以下要求：id唯一
    * */
    private static void addStudent() {
        Student ss = new Student();
        while (true){
            System.out.println("请输入学生id: ");
            String id = sc.next();
            int index = getIndex(id);
            if (index == -1){
                ss.setId(id);
                break;
            }
            System.out.println("学生id: "+id+"已存在，请重新录入");
        }
        inputStudentInfo(ss);
        list.add(ss);
        System.out.println("学生信息录入成功");
    }

    private static void inputStudentInfo(Student ss) {
        System.out.println("请输入学生姓名: ");
        ss.setName(sc.next());
        System.out.println("请输入学生年龄: ");
        ss.setAge(sc.nextInt());
        System.out.println("请输入学生家庭住址: ");
        ss.setAddress(sc.next());
    }

    private static int getIndex(String id){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
}
